package dsk.demo.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dsk.demo.common.utils.PageUtils;
import dsk.demo.common.utils.Query;
import dsk.demo.product.dao.ProductCarDao;
import dsk.demo.product.entity.ProductCarEntity;
import dsk.demo.product.service.ProductCarService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


@Service("productCarService")
public class ProductCarServiceImpl extends ServiceImpl<ProductCarDao, ProductCarEntity> implements ProductCarService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductCarEntity> page = this.page(
                new Query<ProductCarEntity>().getPage(params),
                new QueryWrapper<ProductCarEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 导入车型, 车型导入 只允许  品牌  然后同一品牌的不同车系 全名
     *
     * @param file
     * @return
     */
    @Override
    public List<ProductCarEntity> excel2ProductEntityList(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            Map<String, String> map = new HashMap<>();
            map.put("本厂编号", "productNo");
            map.put("品牌", "brand");
            map.put("车型全名", "fullname");
            reader.setHeaderAlias(map);
            List<ProductCarEntity> all = reader.readAll(ProductCarEntity.class);
            //将分行的oem号码 处理为不同对象.
            List<ProductCarEntity> finalList = new ArrayList<>();
            all.stream().forEach(item -> {
                if (StrUtil.isBlank(item.getFullname()) || StrUtil.isBlank(item.getProductNo())) {
                    return;
                }
                String[] split = item.getFullname().split("\\n");
                if (split.length >= 1) {
                    for (String fullName : split) {
                        if (StrUtil.isBlank(fullName)) {
                            continue;
                        }
                        ProductCarEntity entity = new ProductCarEntity();
                        entity.setProductNo(item.getProductNo());
                        entity.setBrand(item.getBrand());
                        entity.setFullname(fullName);
                        finalList.add(entity);
                    }

                } else {
                    finalList.add(item);
                }
            });
            return all;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public void insertBatch(Set<ProductCarEntity> collect) {
        if (CollectionUtil.isNotEmpty(collect)) {
            Set<ProductCarEntity> collect1 = collect.stream().filter(item -> StrUtil.isNotBlank(item.getFullname())).collect(Collectors.toSet());
            collect1.forEach(item -> {
                item.setBrand(StrUtil.isNotBlank(item.getBrand()) ? item.getBrand().trim() : "MODEL");
                item.setFullname(item.getFullname().trim());
            });
            this.getBaseMapper().insertBatch(collect1);
        }
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<ProductCarEntity> entityList) {
        if (CollectionUtil.isNotEmpty(entityList)) {
            Set<ProductCarEntity> collect1 = entityList.stream().filter(item -> StrUtil.isNotBlank(item.getFullname())).collect(Collectors.toSet());
            collect1.forEach(item -> {
                item.setBrand(StrUtil.isNotBlank(item.getBrand()) ? item.getBrand().trim() : "MODEL");
                item.setFullname(item.getFullname().trim());
            });
            return super.saveOrUpdateBatch(collect1);
        }
        return true;
    }

    @Override
    @Transactional
    public int updateCarProductNo(String oldNo, String newNo) {
        ProductCarEntity entity = new ProductCarEntity();
        entity.setProductNo(newNo);
        UpdateWrapper<ProductCarEntity> wrapper = new UpdateWrapper<ProductCarEntity>();
        wrapper.set("product_no", newNo).eq("product_no", oldNo);
        return this.getBaseMapper().update(entity, wrapper);
    }

}
